<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

//参数1：访问的URL，参数2：post数据(不填则为GET)，参数3：提交的$cookies,参数4：是否返回$cookies
function curl_request($url,$post='',$cookie='', $returnCookie=0){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36');
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
    curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
    curl_setopt($curl, CURLOPT_HEADER, ['Origin: http://moresound.tk']);
    if($post) {
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
    }
    if($cookie) {
        curl_setopt($curl, CURLOPT_COOKIE, $cookie);
    }
    curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($curl);
    if (curl_errno($curl)) {
        return curl_error($curl);
    }
    curl_close($curl);
    if($returnCookie){
        list($header, $body) = explode("\r\n\r\n", $data, 2);
        preg_match_all("/Set\-Cookie:([^;]*);/", $header, $matches);
        $info['cookie']  = substr($matches[1][0], 1);
        $info['content'] = $body;
        return $info;
    }else{
        return $data;
    }
}

/**
 * 统一返回json
 * @param string $msg
 * @param int $code
 * @param array $data
 * @return \think\response\Jsond
 */
function responseData($msg = 'SUCCESS' , $code = 200 ,$data = []){
    return json([
        'msg'   =>  $msg,
        'code'  => $code,
        'data'  => $data,
    ]);
}



function down_mp3($url, $data){
    // curl下载文件
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_HEADER, ['Origin: http://moresound.tk']);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $mp3 = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Errno'.curl_error($ch);
    }

    curl_close($ch);
    // 保存文件到指定路径
    file_put_contents("f:/voice/test.mp3" , $mp3);

    unset($mp3);
}

/**
 * 获取路径
 * @return bool|string
 */
function getDir(){
    //获取磁盘完整路径
    $dir = __DIR__;
    //查找截取
    $str = substr($dir,0,strripos($dir,'application'));
    // 替换斜杠
    $str = explode('\\',$str);
    $string = '';
    foreach ($str as $k => $v){
        $string .= $v.'/';
    }
    // 删除最后一位
    $string = substr($string,0,strlen($string)-1);
//    $string = rtrim($string,'/');
    return $string;
}

function createFile($dir,$name){
    if(!file_exists($dir)){
        return false;
    }
    $file = $dir.$name;
    fopen($file,'a+');
    if(!is_writetable($file)){

    }

}
//创建文件；
function logResult($path,$data){
    file_put_contents($path, '['.date('Y-m-d : h:i:sa',time()).']'.$data."\r\n",FILE_APPEND);
}

//二维码生成

function create_qrcode($url,$size=4)
{
    require_once "../vendor/phpqrcode/phpqrcode.php";

    QRcode::png($url,false,QR_ECLEVEL_L,$size,2,false);
}


//https的post请求
function https_post($url,$data)

{

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);

    if (!empty($data)){

        curl_setopt($curl, CURLOPT_POST, 1);

        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//        curl_setopt($curl, CURLOPT_HTTPHEADER,array(
//            'Content-Type: application/json'
//        ));

    }

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $output = curl_exec($curl);

    curl_close($curl);

    return $output;

}

/*

* 发起GET网络提交

* @params string $url : 网络地址

*/

function https_get($url)

{

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);

    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

    curl_setopt($curl, CURLOPT_HEADER, FALSE) ;

    curl_setopt($curl, CURLOPT_TIMEOUT,60);

    if (curl_errno($curl)) {

        return 'Errno'.curl_error($curl);

    }

    else{$result=curl_exec($curl);}

    curl_close($curl);

    return $result;

}
/*
 * 发起 java的post请求
* @params string $url : 网络地址
 *
 * */
function java_post($url,$data)
{

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);

    if (!empty($data)){

        curl_setopt($curl, CURLOPT_POST, 1);

        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER,array(
            'Content-Type: application/json'
        ));

    }

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $output = curl_exec($curl);

    curl_close($curl);

    return $output;

}


